package com.darwin.darwinUI.controls
{
	import com.darwin.darwinUI.core.UISprite;
	import com.darwin.darwinUI.events.UIEvent;
	import com.darwin.darwinUI.general.DarwinFont;
	
	import flash.display.DisplayObject;
	import flash.filters.DropShadowFilter;
	
	/**
	 * 开/关 按钮  按钮有选中/未选中两种状态 可设置选中/未选中状态的皮肤 如没有设置皮肤 则自动为两种</br>
	 * 状态添加阴影 状态改变会派发 UIEvent.CHANGE事件 
	 * @author vanCopper
	 * 
	 */	
	public class ToggleButton extends LabelButton
	{
		private var _buttonDown:DisplayObject;
		private var _buttonDownSkin:String;
		private var _buttonUp:DisplayObject;
		private var _buttonUpSkin:String;
		private var _selected:Boolean = true;
		//TODO 位图皮肤  MovieClip 皮肤
		public function ToggleButton(text:String="", background:DisplayObject=null, backgroundSkin:String="", font:DarwinFont=null, textTop:Number=0)
		{
			super(text, background, backgroundSkin, font, textTop);
		}
		
		/**
		 *  状态皮肤
		 */		
		private var _statusDisplayObject:UISprite;
		override protected function initUIComponent():void
		{
			super.initUIComponent();
			_statusDisplayObject = new UISprite();
			addChild(_statusDisplayObject);
			selected = false;
		}
		
		override public function onClick():void
		{
			selected = !_selected;
		}
		
		
		
		/**
		 * 按钮选中 
		 */
		public function get buttonDown():DisplayObject
		{
			return _buttonDown;
		}

		/**
		 * @private
		 */
		public function set buttonDown(value:DisplayObject):void
		{
			_buttonDown = value;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 按钮选中皮肤 
		 */
		public function get buttonDownSkin():String
		{
			return _buttonDownSkin;
		}

		/**
		 * @private
		 */
		public function set buttonDownSkin(value:String):void
		{
			_buttonDownSkin = value;
			if(_buttonDownSkin)
			{
				binding(_buttonDownSkin,"buttonDown");
			}
		}

		/**
		 * 按钮未选中
		 */
		public function get buttonUp():DisplayObject
		{
			return _buttonUp;
		}

		/**
		 * @private
		 */
		public function set buttonUp(value:DisplayObject):void
		{
			_buttonUp = value;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 按钮未选中皮肤 
		 */
		public function get buttonUpSkin():String
		{
			return _buttonUpSkin;
		}

		/**
		 * @private
		 */
		public function set buttonUpSkin(value:String):void
		{
			_buttonUpSkin = value;
			if(_buttonUpSkin)
			{
				binding(_buttonUpSkin,"buttonUp");
			}
		}

		/**
		 * 按钮状态 
		 */
		public function get selected():Boolean
		{
			return _selected;
		}

		private var _selectedChange:Boolean = false;
		/**
		 * @private
		 */
		public function set selected(value:Boolean):void
		{
			if(_selected != value)
			{
				_selected = value;

				var uiEvent:UIEvent = new UIEvent(UIEvent.UI_CHANGE);
				uiEvent.data = _selected;
				dispatchEvent(uiEvent);
				_selectedChange = true;
				if(this.stage)this.stage.invalidate();
			}
		}

		override protected function refresh():void
		{
			super.refresh();
			
			if(_selectedChange)
			{
				if(_statusDisplayObject.numChildren)
				{
					_statusDisplayObject.removeAllChild();
				}
				if(selected)
				{
					if(buttonDown)
					{
						_statusDisplayObject.addChild(buttonDown);
					}else
					{
						if(background)
						{
							background.filters = [getShadow(1,selected)];
						}
					}
				}else
				{
					if(buttonUp)
					{
						_statusDisplayObject.addChild(buttonUp);
					}else
					{
						if(background)
						{
							background.filters = [getShadow(1,selected)];
						}
					}
				}
				_selectedChange = false;
			}
		}
		
		/**
		 * 获得阴影滤镜 
		 * @param dist
		 * @param knockout
		 * @return 
		 * 
		 */		
		private function getShadow(dist:Number, knockout:Boolean = false):DropShadowFilter
		{
			return new DropShadowFilter(dist, 45, 0x000000, 1, dist, dist, .3, 1, knockout);
		}

	}
}